//
//  QueryCarViewController.m
//  carcareIOS
//
//  Created by wr on 15/7/18.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "QueryCarViewController.h"
#import "QueryCarCell.h"
#import "NetViolations.h"
#import "NoRulesViewController.h"
#import "RulesListViewController.h"

#define CELL_SECTION_H (65 * __MAIN_RATIO_375)

@interface QueryCarViewController ()<UITableViewDataSource,UITableViewDelegate,BRQueryViewControllerDelegate>

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSArray * dataArray;

@property (nonatomic , strong) ViolationMgr * cityMgr;

@end

@implementation QueryCarViewController
- (instancetype)initWithData:(NSArray *)dataArray withDataManager:(ViolationMgr *)cityMgr
{
    self = [super init];
    if (self) {
        self.cityMgr = cityMgr;
        self.dataArray = dataArray;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommentHelper useFunction:Use_ViolationCars];
    
    self.title = @"违章查询";
    
    __weak __typeof(self) wself = self;
    
    [self setupNavRightImageName:@"addnew_icon" action:^{
        if(![Check isVisiter]){
            [wself editAndAddButtonClick:nil];
        }
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.separatorColor = [Theme lineColor];
    [self.view addSubview:self.tableView];

}
- (void)editAndAddButtonClick:(UIButton  *)sender
{
    NSString * tagStr = ([[NSString stringWithFormat:@"%ld",(long)sender.tag] isEqualToString:@"0"] ? @"" : [NSString stringWithFormat:@"%ld",(long)sender.tag - 50]);
    NSString * queryIDStr = ([tagStr isNotNil] ? [NSString stringWithFormat:@"%zd",((ViolationCarModel *)self.dataArray[[tagStr integerValue]]).cid]: @"0");
    ViolationCarModel * queryDataModel = ([tagStr isNotNil] ? ((ViolationCarModel *)self.dataArray[[tagStr integerValue]]): nil);

    if (![tagStr isNotNil]) {
        BRQueryViewController * breakRulesVC = [[BRQueryViewController alloc] initWithQueryIDStr:@"0" withOtherMessageModel:queryDataModel withDataManager:self.cityMgr];
        breakRulesVC.delegate = self;
        [self.navigationController pushViewController:[breakRulesVC setupNavBack] animated:YES];
        return;
    }
    BRQueryViewController * breakRulesVC = [[BRQueryViewController alloc] initWithQueryIDStr:queryIDStr withOtherMessageModel:queryDataModel withDataManager:self.cityMgr];
    breakRulesVC.delegate = self;
    [self.navigationController pushViewController:[breakRulesVC setupNavBack] animated:YES];

}
#pragma  mark - breakRules Delegate
- (void)configPageReloadData
{
    __weak __typeof(self)wself = self;
    [self.cityMgr updateCarModelsFinish:^(NSArray *carModels) {
        if (carModels.count == 0) {
            [wself.navigationController popViewControllerAnimated:YES];
        }
        wself.dataArray = carModels;
        [wself.tableView reloadData];
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![Check isVisiter]){
        ViolationCarModel * model = [self.dataArray objectAtIndex:indexPath.section];
        __weak __typeof(self) wself = self;
        
        [self.cityMgr checkViolationWithCarInfo:model.modelDic finish:^(NSArray *violationModels) {
            NSString * queryIDStr = [NSString stringWithFormat:@"%zd",((ViolationCarModel *)self.dataArray[indexPath.section]).cid] ;
            if ([violationModels isKindOfClass:[NSNull class]] || violationModels.count == 0) {
                [wself.navigationController pushViewController:[[[NoRulesViewController alloc]initWithQueryID:queryIDStr] setupNavBack] animated:YES];
            }else
            {
                [wself.navigationController pushViewController:[[[RulesListViewController alloc]initWithData:violationModels initWithQueryID:queryIDStr withDataManager:self.cityMgr withCarModel:model] setupNavBack] animated:YES];
            }
        }];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (section == 0 ? 10 : 1);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headView = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, (section == 0 ? 10 : 5)) bgColor:[UIColor clearColor] onView:nil];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [QueryCarCell cellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    QueryCarCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[QueryCarCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ViolationCarModel * model = self.dataArray[indexPath.section];
    
    cell.nameTitleLabel.text = model.carno;
    cell.wordDetailLabel.text = (model.count == 0 ? @"无违章记录" : [NSString stringWithFormat:@"%zd条违章记录待处理",model.count]);
    cell.editButton.tag = indexPath.section + 50;
    [cell.editButton addTarget:self action:@selector(editAndAddButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
